package com.tfswx.ruleengine.compute.gzjy.felfunc;

import com.greenpineyu.fel.context.FelContext;
import com.tfswx.ruleengine.compute.bean.DataObject;
import com.tfswx.ruleengine.compute.bean.GzjyjgItem;
import com.tfswx.ruleengine.compute.gzjy.BaseFn;
import com.tfswx.ruleengine.compute.gzjy.FelService;
import com.tfswx.ruleengine.compute.util.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ricky
 * @date 2021/3/25
 */
public class SetValue extends BaseFn {

    @Override
    public Object call(Object[] arguments, FelContext context) {
        try {
            DataObject dataEntityDTO = (DataObject) arguments[0];
            String arg = String.valueOf(arguments[1]);
            String zdbmz = String.valueOf(arguments[2]);
            String zdwbz = "";
            if (arguments.length == 4) {
                zdwbz = String.valueOf(arguments[3]);
            }

            DataObject.ZdItemDTO zd = getZd(dataEntityDTO, arg, true);
            if (zd == null) {
                return false;
            }

            FelService service = (FelService) context.get("service");
            List<GzjyjgItem> hcjgxqList = service.getHcjgxqList();
            for (GzjyjgItem gzjyjgItem : hcjgxqList) {
                if (gzjyjgItem.getHczdbh().equals(zd.getHczdbh())) {
                    gzjyjgItem.setBghzdbmz(zdbmz);
                    gzjyjgItem.setBghzdwbz(zdwbz);
                    zd.setBghzdbmz(zdbmz);
                    zd.setBghzdwbz(zdwbz);
                    zd.setBgsj(DateUtil.getNowStr());
                }
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public String getZwmc() {
        return "字段赋值";
    }

    @Override
    public boolean isExport() {
        return true;
    }

    @Override
    public boolean isBuiltIn() {
        return true;
    }

    @Override
    public List<Param> getParams() {
        List<Param> params = new ArrayList<>();
        params.add(new Param("$data"));
        params.add(new Param("zdmc"));
        params.add(new Param("zdbmz"));
        params.add(new Param("zdwbz", "Boolean", false));
        return params;
    }

    @Override
    public String getName() {
        return "setValue";
    }
}
